<?php
/**
 * Created by PhpStorm.
 * Author: daly
 * Date: 2020/4/29
 * Time: 18:45
 * TEL : 18665585707  Weacht:awngxiaogang
 * luck: 520
 */
namespace app\admin\controller;

use think\facade\Db;
use think\Request;
use think\facade\View;
use think\facade\Config;
use think\contract\TemplateHandlerInterface;
use think\Session;
use Tool\CryptAES;


class Admin
{

    // 初始化
    protected function initialize()
    {

        View::assign([
            'menu'  => getMenuArr(),
            'username' => session('username')
        ]);

        error_reporting(E_ALL ^ E_NOTICE);
    }
    public function index()
    {

        return View();
    }

    public function login(){



        if(request()->isPost() && request()->isAjax()){

            $username=input('username');
            $password=CryptAES::aesEncode(input('password'));


            $result=\app\admin\model\Admin::field('id,username,store_id,store_json')->where(array('username'=>$username,'password'=>$password))->find();
            if(!empty($result)){
                session('admin_id',$result['id']);
                session('store_id',$result['store_id']);
                session('username',$result['username']);
                session('store_json',$result['store_json']);
                return json(array('status'=>1, 'msg'=>'登录成功','url'=>url('Index/index')->build()));
            }else{

                return json(array('status'=>0, 'msg'=>'登录失败'));
            }
        }

        return   View();
    }

    /***
     * 退出登录
     */
    public function logout(){

        session(null);
        return redirect((string)url('admin/login')->build());
    }

}